using System;
using System.Collections.Generic;
using System.Text;
using UserInterface.Misc;

namespace UserInterface.MML.Base
{
    public class HTMLTagBuilder
    {
        private string name;
        private string content;
        private Dictionary<string, string> attributes =
            new Dictionary<string, string>();

        public HTMLTagBuilder()
        {
        }

        public HTMLTagBuilder(string name)
        {
            this.name = name;
        }

        public HTMLTagBuilder(string name, string content)
        {
            this.name = name;
            this.content = content;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        public Dictionary<string, string> Attributes
        {
            get { return attributes; }
        }

        public string BuildRegularTag()
        {
            return BuildTag(false);
        }

        public string BuildSelfClosingTag()
        {
            return BuildTag(true);
        }
        
        private string BuildTag(bool isSelfClosing)
        {
            StringBuilder tag = new StringBuilder();
            tag.Append("<");
            tag.Append(name);
            foreach (KeyValuePair<string, string> attribute in attributes)
            {
                tag.Append(" ");
                tag.Append(attribute.Key);
                tag.Append("=\"");
                tag.Append(OutputTools.HTMLEntities(attribute.Value));
                tag.Append("\"");
            }
            if (isSelfClosing)
            {
                tag.Append(" /");
            }
            tag.Append(">");
            if (!isSelfClosing)
            {
                tag.Append(content);
                tag.Append("</");
                tag.Append(name);
                tag.Append(">");
            }
            return tag.ToString();
        }

        public override string ToString()
        {
            return BuildRegularTag();
        }
    }
}
